package gui;

import client.ClientControl;
import constant.Constant;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import server.TodoItem;

public class Toolbar extends JToolBar implements ActionListener{
    
    private JButton newBut, refreshBut, deleteBut, editBut;
    private ClientControl client;
    
    public Toolbar(){
        client = ClientControl.instance;
        try {
            ContentMain.instance.addNewTable(client.getListServer());
        } catch (RemoteException ex) {
            Logger.getLogger(Toolbar.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        newBut = new JButton(new ImageIcon(Constant.IMPAGE_PATH+"new.png"));
        newBut.addActionListener(this);

        refreshBut = new JButton(new ImageIcon(Constant.IMPAGE_PATH+"refresh.png"));
        refreshBut.addActionListener(this);
        
        deleteBut = new JButton(new ImageIcon(Constant.IMPAGE_PATH+"delete.png"));
        deleteBut.addActionListener(this);
        
        editBut = new JButton(new ImageIcon(Constant.IMPAGE_PATH+"edit.png"));
        editBut.addActionListener(this);
        
        
        add(newBut);
        add(refreshBut);
        add(editBut);
        add(deleteBut);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==newBut){
            ModifyInfoDialog dialog = new ModifyInfoDialog((JFrame) SwingUtilities.getRoot(this), true);
            try {
                TodoItem item = dialog.showDialog();
                if(item!=null)
                    client.updateList(item);
            } catch (RemoteException ex) {
                Logger.getLogger(Toolbar.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else if(e.getSource()==refreshBut){
            try {
                ContentMain.instance.addNewTable(client.getListServer());
            } catch (RemoteException ex) {
                Logger.getLogger(Toolbar.class.getName()).log(Level.SEVERE, null, ex);
            }
            StatusBar.instance.setStatus("");
        }else if(e.getSource()==deleteBut){
            TodoItem todo = ContentMain.instance.getSelectedRowData();
            if(todo!=null){
                try {
                    client.deleteTodo(todo);
                } catch (RemoteException ex) {
                    Logger.getLogger(Toolbar.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }else if(e.getSource()==editBut){
            TodoItem todo = ContentMain.instance.getSelectedRowData();
            if(todo!=null){
                ModifyInfoDialog dialog = new ModifyInfoDialog(todo, (JFrame) SwingUtilities.getRoot(this), true);
                try {
                    TodoItem item = dialog.showDialog();
                    if(item!=null)
                        client.updateList(item);
                } catch (RemoteException ex) {
                    Logger.getLogger(Toolbar.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
